<?php
/**
 * Created by PhpStorm.
 * User: Jacob Shi
 * Date: 2016/6/22
 * Time: 15:42
 */

class Silk_Mapi_Helper_Gift extends Mage_Core_Helper_Abstract
{
    public function getGiftItems($quote,$parentItem)
    {
        $_imageHelper = Mage::helper('catalog/image');
        $giftItems = Mage::getModel('sales/quote_item')->getCollection()->setQuote($quote)
            ->addFieldToFilter('parent_item_id', array('eq'=>$parentItem->getId()))
            ->addFieldToFilter('gift_id', array('gt'=>0));
        $gifts = array();
        foreach($giftItems as $item)
        {
            $gift = Mage::getModel("pms/gift")->load($item->getGiftId());
            //add by wanglin start
            if($gift->getStatus()==0)continue;
            $gift_start = $gift->getFromDate();
            $gift_end = $gift->getToDate();
            $nowtime = (time()+8*3600);
            if($nowtime>strtotime($gift_end) || $nowtime<strtotime($gift_start))continue;
            //add by wanglin end
            if(!$gift || !$gift) continue;
            $gifts[] = array(
                'item_id'=>$parentItem->getId(),
                'id'=>$gift->getId(),
                'tip'=>$gift->getTip(),
                'qty'=>intval($item->getQty()),
                'gift_name'=>$gift->getGiftProduct()->getName()?:'',
                'gift_product_id'=>$gift->getGiftProductId(),
                'gift_image'=>$_imageHelper->init($gift->getGiftProduct(), 'image')->resize(250) . "",
                'url'=>$gift->getUrl()?:'',
                'description'=>$gift->getDescription()
            );
        }
        return $gifts;
    }

    public function getOrderGiftItems($order, $item)
    {
        $_imageHelper = Mage::helper('catalog/image');
        $giftItems = Mage::getModel('sales/order_item')->getCollection()
            ->addFieldToFilter('order_id', array('eq'=>$order->getId()))
            ->addFieldToFilter('parent_item_id', array('eq'=>$item->getId()))
            ->addFieldToFilter('gift_id', array('gt'=>0));
        $gifts = array();
        foreach($giftItems as $item)
        {
            $gift = Mage::getModel("pms/gift")->load($item->getGiftId());
            //add by wanglin start
            if($gift->getStatus()==0)continue;
            $gift_start = $gift->getFromDate();
            $gift_end = $gift->getToDate();
            $nowtime = (time()+8*3600);
            if($nowtime>strtotime($gift_end) || $nowtime<strtotime($gift_start))continue;
            //add by wanglin end
            if(!$gift || !$gift) continue;
            $gifts[] = array(
                'id'=>$gift->getId(),
                'tip'=>$gift->getTip(),
                'qty'=>intval($item->getQtyOrdered()),
                'gift_name'=>$gift->getGiftProduct()->getName()?:'',
                'gift_product_id'=>$gift->getGiftProductId(),
                'gift_image'=>$_imageHelper->init($gift->getGiftProduct(), 'image')->resize(250) . "",
                'url'=>$gift->getUrl()?:'',
                'description'=>$gift->getDescription()
            );
        }
        return $gifts;
    }
}